package com.example.demo.proxy.controller;

import com.example.demo.proxy.ServiceHandler;
import com.example.demo.proxy.service.HelloService;
import com.example.demo.proxy.service.ProxyService;
import com.example.demo.proxy.service.impl.HelloServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/proxy")
public class ProxyController {

    private ProxyService proxyService;

    /*@Autowired
    public void setProxyService(ProxyService proxyService) {
        this.proxyService = proxyService;
    }*/

    public ProxyController(ProxyService proxyService) {
        this.proxyService = proxyService;
    }

    /*@Autowired
    private ProxyService proxyService;*/

    @RequestMapping("/getProxy/hello")
    public String hello(@RequestParam String name) {
        HelloService o = (HelloService) proxyService.getProxy(new ServiceHandler(new HelloServiceImpl()), HelloService.class);

        //HelloService o = (HelloService) Proxy.newProxyInstance(HelloService.class.getClassLoader(), new Class<?>[]{HelloService.class}, new ServiceHandler(new HelloServiceImpl()));
        return o.sayHello(name);
    }

    @RequestMapping("/getProxy/fk")
    public String fk(@RequestParam String name) {
        HelloService o = (HelloService) proxyService.getProxy(new ServiceHandler(new HelloServiceImpl()), HelloService.class);

        //HelloService o = (HelloService) Proxy.newProxyInstance(HelloService.class.getClassLoader(), new Class<?>[]{HelloService.class}, new ServiceHandler(new HelloServiceImpl()));
        return o.sayFk(name);
    }
}
